package com.xilihui.xlh.business.requests;

import android.content.Context;

import com.xilihui.xlh.business.entities.LoginEntity;
import com.xilihui.xlh.constant.StrConst;
import com.xilihui.xlh.core.app.BaseEntity;
import com.xilihui.xlh.core.http.HttpManager;
import com.xilihui.xlh.core.util.SPUtil;
import com.xilihui.xlh.core.util.SignUtil;
import com.xilihui.xlh.core.util.TimeFormatUtil;

import java.util.HashMap;

import rx.Observable;

/**
 * Created by Administrator on 2018/6/20.
 */

public class LoginRequest {
    
    public static LoginService loginService = HttpManager.getRetrofit().create(LoginService.class);

    /**
     * 登录
     * @param mobile
     * @param password
     * @return
     */
    public static Observable<LoginEntity> login(Context context,String mobile, String password){
        
        String app_key = StrConst.app_key;
        String temptime = TimeFormatUtil.getTimestamp();
        
        String device_token = (String)SPUtil.get(context,SPUtil.DEVICE_TOKEN,"");
        
        HashMap<String,String> map = new HashMap<>();
        map.put("mobile",mobile);
        map.put("password",password);
        map.put("app_key",app_key);
        map.put("temptime",temptime);
        map.put("device_token",device_token);
        
        return loginService.login(mobile,password,app_key,temptime,SignUtil.sign(map),device_token);
    }

    /**
     * 短信登录
     * @param mobile
     * @param code
     * @return
     */
    public static Observable<LoginEntity> smsLogin(Context context,String mobile,String code){
        
        String app_key = StrConst.app_key;
        String temptime = TimeFormatUtil.getTimestamp();

        String device_token = (String)SPUtil.get(context,SPUtil.DEVICE_TOKEN,"");
        
        HashMap<String,String> map = new HashMap<>();
        map.put("mobile",mobile);
        map.put("code",code);
        map.put("app_key",app_key);
        map.put("temptime",temptime);
        map.put("device_token",device_token);
        return loginService.smsLogin(mobile,code,app_key,temptime,SignUtil.sign(map),device_token);
    }

    /**
     * 注册
     * @param mobile
     * @param password
     * @param repassword
     * @param code
     * @param app_key
     * @return
     */
    public static Observable<BaseEntity> reg(String mobile,String password, String repassword, 
                                             String code, String app_key){
        String temptime = TimeFormatUtil.getTimestamp();

        HashMap<String,String> map = new HashMap<>();
        map.put("mobile",mobile);
        map.put("password",password);
        map.put("repassword",repassword);
        map.put("code",code);
        map.put("app_key",app_key);
        map.put("temptime",temptime);
        
        return loginService.reg(mobile,password,repassword,code,app_key,temptime, SignUtil.sign(map));
    }

    /**
     * 发送验证码
     * @param mobile
     * @param scene
     * @return
     */
    public static Observable<BaseEntity> sendCode(String mobile,String scene){
        return loginService.sendCode(mobile,scene);
    }

    /**
     * 忘记密码
     * @param mobile
     * @param code
     * @return
     */
    public static Observable<LoginEntity> forget(String mobile,String code){
        
        String temptime = TimeFormatUtil.getTimestamp();
        String app_key = StrConst.app_key;
        
        HashMap<String,String> map = new HashMap<>();
        map.put("mobile",mobile);
        map.put("app_key",app_key);
        map.put("code",code);
        map.put("scene",StrConst.scene_gxmm);
        map.put("temptime",temptime);
        
        return loginService.forget(mobile,app_key,code,StrConst.scene_gxmm,temptime,SignUtil.sign(map));
    }

    /**
     * 忘记密码 设置密码
     * @param access_token
     * @param password
     * @param repassword
     * @param user_id
     * @return
     */
    public static Observable<BaseEntity> forgetNext(String access_token,String password,String repassword
    ,String user_id){
        
        String app_key = StrConst.app_key;
        String temptime = TimeFormatUtil.getTimestamp();
        
        HashMap<String,String> map = new HashMap<>();
        map.put("access_token",access_token);
        map.put("app_key",app_key);
        map.put("user_id",user_id);
        map.put("password",password);
        map.put("repassword",repassword);
        map.put("temptime",temptime);
        
        return loginService.forgetNext(access_token,app_key,user_id,password,repassword,temptime,SignUtil.sign(map));
    }
    
}
